/*
 *  Copyright (C) 2011 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.nbpam.components;

import java.awt.Color;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import org.balisunrise.application.Application;
import org.balisunrise.application.Module;
import org.balisunrise.common.IconManager;

/**
 *
 * @author glauco
 */
public class ModulePanel extends JPanel{

    private Module module;
    private JLabel description;
    private JLabel imageLabel;

    public ModulePanel(Module module) {
        this.module = module;
        init();
    }

    public Module getModule() {
        return module;
    }

    public void setModule(Module module) {
        this.module = module;
        init();
    }

    private void init(){
        
        removeAll();

        setBorder(new SoftBevelBorder(BevelBorder.RAISED));
        addMouseListener(new java.awt.event.MouseAdapter() {
            Color bg;
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                if(evt.getButton() == MouseEvent.BUTTON1){
                    ModuleMenuTopComponent win =
                        ModuleMenuTopComponent.findInstance(module);
                    win.open();
                    win.requestActive();
                }
            }
            @Override
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                if(bg == null)
                    bg = getBackground();
                setBackground(bg.darker());
            }
            @Override
            public void mouseExited(java.awt.event.MouseEvent evt) {
                if(bg != null)
                    setBackground(bg);
            }
        });

        description = new JLabel(module.getDescription());
        description.setFont(new java.awt.Font("Tahoma", 1, 12));

        IconManager<ImageIcon> im = Application.get().getIconManager();

        imageLabel = new JLabel(im.getIcon(module.getLargeIcon()));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(imageLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(description, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(description)
                    .addComponent(imageLabel))//, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

    }
}
